const merge = require('webpack-merge');
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const baseWebpackConfig = require('./webpack.base.conf');
const CompressionPlugin = require('compression-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;



const webpackConfig = merge(baseWebpackConfig, {
  //environment specific config goes here
  mode: 'production',
  // devtool: 'inline-source-map',
  // devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.(le|c)ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
            //   sourceMap: false,
              modules: false,
              // localIdentName: '[name]---[local]---[hash:base64:5]'
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: [require("autoprefixer")],
              sourceMap: false
            }
          },
          {
            loader: 'less-loader'
          }
        ]
      }
    ]
  },
  // optimization: {
  //   // runtimeChunk: true,
  //   splitChunks: {
  //     chunks: "all"
  //   }
  // },

  optimization: {
    concatenateModules: true,
    runtimeChunk: {
      "name": "runtime"
    },
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
          common: {
              minChunks: 2,
              name: 'commons',
              chunks: 'async',
              priority: 10,
              reuseExistingChunk: true,
              enforce: true
          }
      }
    }
  },
  plugins: [
    new webpack.HashedModuleIdsPlugin(),
    new webpack.NamedModulesPlugin(),
    new MiniCssExtractPlugin({
      filename: "css/[name].[contenthash].css",
      chunkFilename: 'css/[name].[contenthash:8].css'
    }),
    new BundleAnalyzerPlugin({
      openAnalyzer: false,
      analyzerMode: 'static',
      reportFilename: 'bundle-analyzer-report.html'
    }),
    new CompressionPlugin({
      filename: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(js|css)$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })

  ]
});

module.exports = webpackConfig;
